<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\ProjectDB;
use Illuminate\Http\Request;

class ProjectController extends BaseController
{
    /**
     *
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new ProjectDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $dataArr = $this->db->getIndex();
        if (!$dataArr) {
            return $this->toAppJson(-4);
        }
        return $this->toAppJson(0,$dataArr);
    }

    /**
     * 详情
     */
    public function getOne(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $proId = $request->post('proId',0);
        $dataArr = $this->db->getOne($proId);
        if (!$dataArr) {
            return $this->toAppJson(-4);
        }
        return $this->toAppJson(0,$dataArr);
    }

    /**
     * 新增
     */
    public function setAdd(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $name = $request->post('name','');
        if (!$name) {
            return $this->toAppJson(-3);
        }
        $resDb = $this->db->setAdd($adminId,$name);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5);
        }
        return $this->toAppJson(0);
    }

    /**
     * 更新
     */
    public function setDetail(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-2);
        }
        $proId = $request->post('proId','');
        $detail = $request->post('detail','');
        if (!$proId || !$detail) {
            return $this->toAppJson(-3);
        }
        $resDb = $this->db->setDetail($proId,$detail);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-9);
        }
        return $this->toAppJson(0);
    }
}
